IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'MD3.f_GetColumnDataType'))
   DROP FUNCTION MD3.f_GetColumnDataType
GO

CREATE FUNCTION MD3.f_GetColumnDataType (
    @SchemaName SYSNAME
   ,@TableName SYSNAME
   ,@ColumnName SYSNAME
   )
    RETURNS SYSNAME
    WITH RETURNS NULL ON NULL INPUT
AS

BEGIN

    DECLARE @ColumnDataType SYSNAME

    SELECT @ColumnDataType = TYPE_NAME(c.system_type_id)
    FROM sys.objects o
    JOIN sys.columns c 
      ON o.object_id = c.object_id
    JOIN sys.types t 
      ON c.system_type_id = t.system_type_id
    WHERE o.name = @TableName 
      AND c.name = @ColumnName 
      AND o.schema_id = schema_id(@SchemaName)
      AND o.type = 'U'

    RETURN @ColumnDataType

END
GO
